import { get, post, put, del } from '@/utils/request'

/**
 * 财务相关API
 */

// 获取财务概览
export function getFinanceOverview(period = 'month') {
  return get('/finance/overview', { period }, {
    showLoading: false
  })
}

// 获取收入列表
export function getIncomeList(params = {}) {
  return get('/finance/income', params, {
    showLoading: false
  })
}

// 获取支出列表
export function getExpenseList(params = {}) {
  return get('/finance/expense', params, {
    showLoading: false
  })
}

// 添加收入记录
export function createIncome(data) {
  return post('/finance/income', data, {
    showLoading: true,
    loadingText: '添加收入记录...'
  })
}

// 添加支出记录
export function createExpense(data) {
  return post('/finance/expense', data, {
    showLoading: true,
    loadingText: '添加支出记录...'
  })
}

// 更新收入记录
export function updateIncome(id, data) {
  return put(`/finance/income/${id}`, data, {
    showLoading: true,
    loadingText: '更新收入记录...'
  })
}

// 更新支出记录
export function updateExpense(id, data) {
  return put(`/finance/expense/${id}`, data, {
    showLoading: true,
    loadingText: '更新支出记录...'
  })
}

// 删除收入记录
export function deleteIncome(id) {
  return del(`/finance/income/${id}`, {}, {
    showLoading: true,
    loadingText: '删除收入记录...'
  })
}

// 删除支出记录
export function deleteExpense(id) {
  return del(`/finance/expense/${id}`, {}, {
    showLoading: true,
    loadingText: '删除支出记录...'
  })
}

// 获取财务报表
export function getFinanceReport(params = {}) {
  return get('/finance/report', params, {
    showLoading: true,
    loadingText: '生成财务报表...'
  })
}

// 获取账户余额详情
export function getAccountBalance() {
  return get('/finance/balance')
}

// 申请提现
export function applyWithdraw(data) {
  return post('/finance/withdraw/apply', data, {
    showLoading: true,
    loadingText: '提交提现申请...'
  })
}

// 获取提现记录
export function getWithdrawList(params = {}) {
  return get('/finance/withdraw/list', params)
}

// 取消提现申请
export function cancelWithdraw(withdrawId) {
  return put(`/finance/withdraw/${withdrawId}/cancel`, {}, {
    showLoading: true,
    loadingText: '取消提现申请...'
  })
}

// 获取交易记录
export function getTransactionList(params = {}) {
  return get('/finance/transactions', params)
}

// 导出交易记录
export function exportTransactions(params = {}) {
  return get('/finance/transactions/export', params, {
    responseType: 'blob'
  })
}

// 获取风控信息
export function getRiskInfo() {
  return get('/finance/risk')
}

// 获取冻结资金详情
export function getFrozenDetails() {
  return get('/finance/frozen/details')
}

// 获取在途资金详情
export function getPendingDetails() {
  return get('/finance/pending/details')
}

// 获取收入统计
export function getIncomeStats(timeRange = 'month') {
  return get('/finance/income/stats', { timeRange })
}

// 获取财务分析数据
export function getFinanceAnalysis(params = {}) {
  return get('/finance/analysis', params)
}

// 获取结算记录
export function getSettlementList(params = {}) {
  return get('/finance/settlement', params)
}

// 手动触发结算
export function triggerSettlement(orderIds) {
  return post('/finance/settlement/trigger', { orderIds }, {
    showLoading: true,
    loadingText: '触发结算...'
  })
}

// 获取提现配置
export function getWithdrawConfig() {
  return get('/finance/withdraw/config')
}

// 更新提现配置
export function updateWithdrawConfig(config) {
  return put('/finance/withdraw/config', config, {
    showLoading: true,
    loadingText: '更新提现配置...'
  })
}

// 获取银行卡列表
export function getBankCardList() {
  return get('/finance/bank-cards')
}

// 添加银行卡
export function addBankCard(data) {
  return post('/finance/bank-cards', data, {
    showLoading: true,
    loadingText: '添加银行卡...'
  })
}

// 删除银行卡
export function deleteBankCard(cardId) {
  return del(`/finance/bank-cards/${cardId}`, {}, {
    showLoading: true,
    loadingText: '删除银行卡...'
  })
}

// 验证银行卡
export function verifyBankCard(cardId, data) {
  return post(`/finance/bank-cards/${cardId}/verify`, data, {
    showLoading: true,
    loadingText: '验证银行卡...'
  })
}

// 财务状态常量
export const TRANSACTION_TYPES = {
  INCOME: 'income',
  WITHDRAW: 'withdraw',
  FREEZE: 'freeze',
  UNFREEZE: 'unfreeze',
  REFUND: 'refund',
  SETTLEMENT: 'settlement'
}

export const TRANSACTION_STATUS = {
  PENDING: 'pending',
  PROCESSING: 'processing',
  COMPLETED: 'completed',
  FAILED: 'failed',
  CANCELLED: 'cancelled'
}

export const WITHDRAW_METHODS = {
  BANK: 'bank',
  ALIPAY: 'alipay',
  WECHAT: 'wechat'
}

// 状态文本映射
export const TRANSACTION_TYPE_TEXT = {
  [TRANSACTION_TYPES.INCOME]: '收入',
  [TRANSACTION_TYPES.WITHDRAW]: '提现',
  [TRANSACTION_TYPES.FREEZE]: '冻结',
  [TRANSACTION_TYPES.UNFREEZE]: '解冻',
  [TRANSACTION_TYPES.REFUND]: '退款',
  [TRANSACTION_TYPES.SETTLEMENT]: '结算'
}

export const TRANSACTION_STATUS_TEXT = {
  [TRANSACTION_STATUS.PENDING]: '待处理',
  [TRANSACTION_STATUS.PROCESSING]: '处理中',
  [TRANSACTION_STATUS.COMPLETED]: '已完成',
  [TRANSACTION_STATUS.FAILED]: '失败',
  [TRANSACTION_STATUS.CANCELLED]: '已取消'
}

export const WITHDRAW_METHOD_TEXT = {
  [WITHDRAW_METHODS.BANK]: '银行卡',
  [WITHDRAW_METHODS.ALIPAY]: '支付宝',
  [WITHDRAW_METHODS.WECHAT]: '微信'
}

// ==================== 洗护系统特有的财务API ====================

// 获取资金余额信息
export function getBalanceInfo() {
  return get('/finance/balance/info')
}

// 获取风控状态
export function getRiskStatus() {
  return get('/finance/risk/status')
}

// 获取交易详情
export function getTransactionDetail(transactionId) {
  return get(`/finance/transactions/${transactionId}`)
}

// 设置默认银行卡
export function setDefaultBankCard(cardId) {
  return put(`/finance/bank-cards/${cardId}/default`)
}

// 验证提现条件
export function validateWithdraw(amount) {
  return post('/finance/withdraw/validate', { amount })
}

// 获取提现规则
export function getWithdrawRules() {
  return get('/finance/withdraw/rules')
}

// 获取支付方式配置
export function getPaymentMethods() {
  return get('/finance/payment-methods')
}

// 更新支付方式配置
export function updatePaymentMethod(method, data) {
  return put(`/finance/payment-methods/${method}`, data)
}

// 导出财务报表
export function exportFinanceReport(params = {}) {
  return get('/finance/report/export', params, {
    responseType: 'blob'
  })
}

// 获取结算详情
export function getSettlementDetail(settlementId) {
  return get(`/finance/settlement/${settlementId}`)
}

// 获取保证金状态
export function getDepositStatus() {
  return get('/finance/deposit/status')
}

// 缴纳保证金
export function payDeposit(data) {
  return post('/finance/deposit/pay', data, {
    showLoading: true,
    loadingText: '缴纳保证金...'
  })
}

// 申请保证金退还
export function applyDepositRefund(data) {
  return post('/finance/deposit/refund', data, {
    showLoading: true,
    loadingText: '申请保证金退还...'
  })
}

// 获取保证金记录
export function getDepositRecords(params = {}) {
  return get('/finance/deposit/records', params)
}

// 获取罚款记录
export function getPenaltyRecords(params = {}) {
  return get('/finance/penalty/records', params)
}

// 申请罚款申诉
export function appealPenalty(penaltyId, data) {
  return post(`/finance/penalty/${penaltyId}/appeal`, data, {
    showLoading: true,
    loadingText: '提交申诉...'
  })
}

// 获取服务费率配置
export function getServiceFeeConfig() {
  return get('/finance/service-fee/config')
}

// 获取平台抽成记录
export function getPlatformCommissionRecords(params = {}) {
  return get('/finance/commission/records', params)
}

// 颜色映射
export const TRANSACTION_TYPE_COLOR = {
  [TRANSACTION_TYPES.INCOME]: 'success',
  [TRANSACTION_TYPES.WITHDRAW]: 'primary',
  [TRANSACTION_TYPES.FREEZE]: 'danger',
  [TRANSACTION_TYPES.UNFREEZE]: 'success',
  [TRANSACTION_TYPES.REFUND]: 'warning',
  [TRANSACTION_TYPES.SETTLEMENT]: 'info'
}

export const TRANSACTION_STATUS_COLOR = {
  [TRANSACTION_STATUS.PENDING]: 'warning',
  [TRANSACTION_STATUS.PROCESSING]: 'primary',
  [TRANSACTION_STATUS.COMPLETED]: 'success',
  [TRANSACTION_STATUS.FAILED]: 'danger',
  [TRANSACTION_STATUS.CANCELLED]: 'info'
}

// 风控等级常量
export const RISK_LEVELS = {
  LOW: 'low',
  MEDIUM: 'medium',
  HIGH: 'high'
}

export const RISK_LEVEL_TEXT = {
  [RISK_LEVELS.LOW]: '低风险',
  [RISK_LEVELS.MEDIUM]: '中风险',
  [RISK_LEVELS.HIGH]: '高风险'
}

export const RISK_LEVEL_COLOR = {
  [RISK_LEVELS.LOW]: 'success',
  [RISK_LEVELS.MEDIUM]: 'warning',
  [RISK_LEVELS.HIGH]: 'danger'
}

// 银行列表常量
export const BANK_LIST = [
  { code: 'ICBC', name: '中国工商银行' },
  { code: 'CCB', name: '中国建设银行' },
  { code: 'ABC', name: '中国农业银行' },
  { code: 'BOC', name: '中国银行' },
  { code: 'CMB', name: '招商银行' },
  { code: 'BOCOM', name: '交通银行' },
  { code: 'CITIC', name: '中信银行' },
  { code: 'CEB', name: '光大银行' },
  { code: 'HXB', name: '华夏银行' },
  { code: 'CMBC', name: '民生银行' },
  { code: 'CGB', name: '广发银行' },
  { code: 'SPDB', name: '浦发银行' },
  { code: 'PSBC', name: '邮储银行' }
]
